package cn.zhz.springdoc.springboot.util;

import cn.hutool.extra.spring.SpringUtil;
import lombok.extern.slf4j.Slf4j;

import java.net.Inet4Address;
import java.net.UnknownHostException;

/**
 * @author ZHZ
 * @since 2022-11-18
 */

@Slf4j
public class PropertyUtil {

    /**
     * 获取上下文
     *
     * @return
     */
    public static String getContextPath() {
        String contextPath = SpringUtil.getProperty("server.servlet.context-path");
        return contextPath == null ? "" : contextPath;
    }

    /**
     * 获取端口号
     *
     * @return
     */
    public static String getPort() {
        return SpringUtil.getProperty("server.port");
    }

    /**
     * 获取host
     *
     * @return
     */
    public static String getHost() {

        String hostAddress = "localhost";
        try {
            hostAddress = Inet4Address.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            log.error("获取主机名失败已默认使用 localhost：", e);
        }
        return hostAddress;
    }


}
